<template>
  <!--文字资料新增/编辑弹窗-->
  <a-modal  :maskClosable="false"
            :title="mdl.dialogTitleMap[mdl.dialogStatus]"
            :width="1000"
            @ok="mdl.dialogStatus === 'create' ? createData() : updateData()"
            v-model="mdl.visible"
            :confirm-loading="loading"
  >
    <a-form-model :model="mdl.formData" :rules="rules">
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="资料标题"
        prop="charName"
      >
        <a-input placeholder="" v-model="mdl.formData.charName"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="资料内容"
        prop="charInfo"
      >
        <wangeditor @catchData="catchData" :editorContent="mdl.formData.charInfo"></wangeditor>
        <!--<a-textarea :rows="10" v-model="mdl.formData.charInfo"/>-->
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="资料积分"
        prop="charIntegral"
      >
        <a-input-number v-model="mdl.formData.charIntegral" :min="1"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="学时"
        prop="period"
      >
        <a-input-number v-model="mdl.formData.period" :min="1"/>
      </a-form-model-item>
      <a-form-model-item
        :labelCol="mdl.labelCol"
        :wrapperCol="mdl.wrapperCol"
        label="相关资料"
        prop="docId"
      >
        <a-select v-model="mdl.formData.docId" allowClear>
          <a-select-option :value="item.id" v-for="(item, index) in flfgList">{{item.name}}</a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
    <div>
    </div>
  </a-modal>
</template>
<script>
  import moment from 'moment';
  import wangeditor from '@/components/wangeditor'
  import {infoDocIquery,studycharinsert,studycharup} from '@/api/invoice'
  export default {
    components: {
      wangeditor
    },
    props: {
      /*   -------弹窗--------*/
      mdl:[Array,Object]
    },
    data() {
      return {
        flfgList: [],
        loading: false,
        rules: {
          charName: [{ required: true, message: '资料名称不能为空', trigger: 'bluer' }],
          charInfo: [{ required: true, message: '资料内容不能为空', trigger: 'bluer' }],
          charIntegral: [{ required: true, message: '资料积分不能为空', trigger: 'bluer' }],
          period: [{ required: true, message: '资料学时不能为空', trigger: 'bluer' }],
          docId: [{ required: true, message: '绑定法律法规不能为空', trigger: 'bluer' }],
        }
      }
    },
    inject: ['upShow'],
    created() {
      this.getFlfgList()
    },
    watch: {

    },
    computed: {

    },
    methods: {
//        查询法律法规名称
      getFlfgList() {
        const parameter = {
          _search: false,//是否分页查询
          pageSize: '',
          pageNumber: '',
          bgTm: "",
          data: {
            courseCount: "",
            id: '',
            intro: "",
            name: ''
          },
          endTm: "",
        }
        infoDocIquery(parameter).then(res => {
          this.flfgList = res.data.records
        }).
        catch(err => {
          this.loading = false
        })
      },
//          新增
      createData() {
          this.loading = true
        const parameter = {
          charInfo: this.mdl.formData.charInfo,
          charIntegral: this.mdl.formData.charIntegral,
          period: this.mdl.formData.period,
          charName: this.mdl.formData.charName,
          infoDocIList: [
            {
              id: this.mdl.formData.docId,
            }
          ],
          uploadTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
        }
        studycharinsert(parameter).then(res => {
            this.loading = false
          if(res.code === 200) {
            this.upShow(1,"新增成功")
            this.mdl.visible = false
            this.$emit('handleSearch')
          } else {
            this.upShow(2,res.msg)
          }
        }).catch(err => {
          this.loading = false
          this.upShow(2)
        })
      },
//      编辑
      updateData() {
        this.loading = true
        const parameter = {
          charInfo: this.mdl.formData.charInfo,
          charIntegral: this.mdl.formData.charIntegral,
          charName: this.mdl.formData.charName,
          period: this.mdl.formData.period,
          id: this.mdl.formData.id,
          infoDocIList: [
            {
              id: this.mdl.formData.docId,
            }
          ],
        }
        studycharup(parameter).then(res => {
          this.loading = false
          if(res.code === 200) {
            this.upShow(1,"编辑成功")
            this.mdl.visible = false
            this.$emit('handleSearch')
          } else {
            this.upShow(2,res.msg)
          }
        }).catch(err => {
          this.loading = false
          this.upShow(2)
        })
      },
//      富文本编辑器传输的数据
      catchData(val) {
        this.mdl.formData.charInfo = val
        console.log('val:'+val)
      },
    }
  }
</script>